package com.jari.lg.service.analysis.checkpoint;

import com.jari.lg.domain.*;
import com.jari.lg.mapper.*;
import com.jari.lg.utils.BaseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author ge
 * description 编写卡口相关统计分析结果数据库数据流程
 */
@Service
public class CheckPointService {

    @Autowired
    private CheckPointHourMapper checkpointHourMapper;

    @Autowired
    private CheckpointDayMapper checkpointDayMapper;

    @Autowired
    private CheckpointMonthMapper checkpointMonthMapper;

    @Autowired
    private CheckpointRtMapper checkpointRtMapper;

    /**
     * 每分钟进行实时卡口数据入库
     */
    //@Scheduled(cron = "* */1 * * * ?")
    public void setCheckpointRtInfo() {
        List<CheckpointRt> result = new ArrayList<>();
        List<DevBase> checkpointList = BaseData.checkpointList;
        //对于每一个卡口，每分钟设置0-20条过车数据
        int length = checkpointList.size();
        for (int i = 0; i < length; i++) {
            generateCheckpointDataIntoRtList(result, checkpointList.get(i), 0, 20);
        }
        checkpointRtMapper.insertBatch(result);
        result.clear();
    }


    /**
     * 向checkpoint_month表格添加月卡口数据
     * 每月第最后一天23:30开始执行
     */
    //@Scheduled(cron = "* * 23 L * ?")
    public void setCheckpointMonthInfo(Date currentDate) {
        List<CheckpointMonth> result = new ArrayList<>();
        List<DevBase> checkpointList = BaseData.checkpointList;
        //2.对于每一个卡口编号，设置9000-150000条过车数据
        int length = checkpointList.size();
        for (int i = 0; i < length; i++) {
            //随机生成300 - 5000 之间的一个数
            int randomNum = (int) (Math.random() * 141001 + 9000);
            generateCheckpointDataIntoMonthList(result, checkpointList.get(i), randomNum, currentDate);
        }
        checkpointMonthMapper.insertBatch(result);

        result.clear();
    }


    /**
     * 设置卡口小时数据
     */
    public void setCheckpointHourInfo(Date currentDay) {
        List<CheckPointHour> result = new ArrayList<>();
        List<DevBase> checkpointList = BaseData.checkpointList;
        //2.对于每一个卡口编号，每小时设置20-500辆过车
        int length = checkpointList.size();
        for (int i = 0; i < length; i++) {
            //随机生成20- 500 之间的一个数
            int randomNum = (int) (Math.random() * 481 + 20);
            generateCheckpointHourList(result,checkpointList.get(i),randomNum,currentDay);
        }
        checkpointHourMapper.insertBatch(result);
        result.clear();
    }

    /**
     * 向checkpoint_day表格，按天添加卡口过车数据，每天增加N条卡口过车数据
     * 该条数据产生每天晚上22点开始
     */
    //@Scheduled(cron = "* * 22 * * ?")
    public void setDayCheckpointDayInfo(Date currentDay) {
        List<CheckpointDay> result = new ArrayList<>();

        //获取所有卡口编号，对于每个卡口，随机产生50-100条过车数据
        //1.获取所有卡口（dev_base表格，deviceType是01）
        List<DevBase> checkpointList = BaseData.checkpointList;
        //2.对于每一个卡口编号，设置300-5000辆过车
        int length = checkpointList.size();
        for (int i = 0; i < length; i++) {
            //随机生成300 - 5000 之间的一个数
            int randomNum = (int) (Math.random() * 4701 + 300);
            generateCheckpointDataIntoDayList(result, checkpointList.get(i), randomNum, currentDay);
        }
        //3.把所有产生的实体对象 批量 插入到表格中
        checkpointDayMapper.insertBatch(result);

        //4.插入到表格中后，释放result list
        result.clear();
    }


    /**
     * 对于每一个卡口，随机生成number个卡口数据，并存到最后的结果list中
     *
     * @param checkpointEntity
     * @param min              最小过车数
     * @param max              最大过车数
     */
    private void generateCheckpointDataIntoRtList(List<CheckpointRt> result, DevBase checkpointEntity, int min, int max) {
        CheckpointRt checkpointRt = new CheckpointRt();
        checkpointRt.setCheckpointcode(checkpointEntity.getDevicecode());
        checkpointRt.setCheckpointname(checkpointEntity.getDevicename());
        checkpointRt.setPassingcarnum(min + (int) Math.random() * (max - min + 1));
        checkpointRt.setDistrictpassingcarnum((int) (0.6 * checkpointRt.getPassingcarnum()));
        checkpointRt.setDatetime(new Date());
        checkpointRt.setAvgspeed(20 + (Math.random() * 41));//平均车速：20-60
        checkpointRt.setLocalcarnum((int) (0.9 * checkpointRt.getDistrictpassingcarnum()));
        checkpointRt.setNolocalcarnum(checkpointRt.getDistrictpassingcarnum() - checkpointRt.getLocalcarnum());
        checkpointRt.setSmallcarnum((int) (0.65 * checkpointRt.getDistrictpassingcarnum()));
        checkpointRt.setBigcarnum(checkpointRt.getDistrictpassingcarnum() - checkpointRt.getSmallcarnum());
        checkpointRt.setPetrolcarnum((int) (0.85 * checkpointRt.getDistrictpassingcarnum()));
        checkpointRt.setElectriccarnum(checkpointRt.getDistrictpassingcarnum() - checkpointRt.getPetrolcarnum());
        checkpointRt.setNormalcarnum((int) (0.95 * checkpointRt.getDistrictpassingcarnum()));
        checkpointRt.setSpecialcarnum(checkpointRt.getDistrictpassingcarnum() - checkpointRt.getNormalcarnum());
        //存入list中
        result.add(checkpointRt);
    }

    public void generateCheckpointHourList(List<CheckPointHour> resultList, DevBase checkpointEntity, int randomNum, Date currentDate) {
        CheckPointHour checkpointDay = new CheckPointHour();
        checkpointDay.setCheckpointcode(checkpointEntity.getDevicecode());
        checkpointDay.setCheckpointname(checkpointEntity.getDevicename());
        checkpointDay.setPassingcarnum(randomNum);
        checkpointDay.setDistrictpassingcarnum((int) (0.6 * checkpointDay.getPassingcarnum()));
        checkpointDay.setStartdatetime(currentDate);
        checkpointDay.setLocalcarnum((int) (0.9 * checkpointDay.getDistrictpassingcarnum()));
        checkpointDay.setNolocalcarnum(checkpointDay.getDistrictpassingcarnum() - checkpointDay.getLocalcarnum());
        checkpointDay.setPetrolcarnum((int) (0.85 * checkpointDay.getDistrictpassingcarnum()));
        checkpointDay.setSmallcarnum((int) (0.65 * checkpointDay.getPetrolcarnum()));
        checkpointDay.setBigcarnum(checkpointDay.getPetrolcarnum() - checkpointDay.getSmallcarnum());
        checkpointDay.setElectriccarnum(checkpointDay.getDistrictpassingcarnum() - checkpointDay.getPetrolcarnum());
        checkpointDay.setNormalcarnum((int) (0.95 * checkpointDay.getDistrictpassingcarnum()));
        checkpointDay.setSpecialcarnum(checkpointDay.getDistrictpassingcarnum() - checkpointDay.getNormalcarnum());
        //存入list中
        resultList.add(checkpointDay);
    }


    /**
     * 对于每一个卡口，随机生成number个卡口数据，并存到最后的结果list中
     *
     * @param resultList
     * @param checkpointEntity
     */
    public void generateCheckpointDataIntoDayList(List<CheckpointDay> resultList, DevBase checkpointEntity, int randomNum, Date currentDay) {
        //天总过车数设置为200到2000之间的一个数；运行车辆数设置为总过车数60%;其他中类型都是9:1 的比例
        //获取卡口所需数据
        CheckpointDay checkpointDay = new CheckpointDay();
        checkpointDay.setCheckpointcode(checkpointEntity.getDevicecode());
        checkpointDay.setCheckpointname(checkpointEntity.getDevicename());
        checkpointDay.setPassingcarnum(randomNum);
        checkpointDay.setDistrictpassingcarnum((int) (0.6 * checkpointDay.getPassingcarnum()));
        checkpointDay.setDate(currentDay);
        checkpointDay.setLocalcarnum((int) (0.9 * checkpointDay.getDistrictpassingcarnum()));
        checkpointDay.setNolocalcarnum(checkpointDay.getDistrictpassingcarnum() - checkpointDay.getLocalcarnum());
        checkpointDay.setPetrolcarnum((int) (0.85 * checkpointDay.getDistrictpassingcarnum()));
        checkpointDay.setSmallcarnum((int) (0.65 * checkpointDay.getPetrolcarnum()));
        checkpointDay.setBigcarnum(checkpointDay.getPetrolcarnum() - checkpointDay.getSmallcarnum());
        checkpointDay.setElectriccarnum(checkpointDay.getDistrictpassingcarnum() - checkpointDay.getPetrolcarnum());
        checkpointDay.setNormalcarnum((int) (0.95 * checkpointDay.getDistrictpassingcarnum()));
        checkpointDay.setSpecialcarnum(checkpointDay.getDistrictpassingcarnum() - checkpointDay.getNormalcarnum());
        //存入list中
        resultList.add(checkpointDay);
    }

    /**
     * 对于每一个卡口，随机生成number个卡口数据，并存到最后的结果list中
     *
     * @param resultList
     * @param checkpointEntity
     */
    public void generateCheckpointDataIntoMonthList(List<CheckpointMonth> resultList, DevBase checkpointEntity, int randomNum, Date currentDay) {
        //天总过车数设置为200到2000之间的一个数；运行车辆数设置为总过车数60%;其他中类型都是9:1 的比例

        //获取卡口所需数据
        CheckpointMonth checkpointMonth = new CheckpointMonth();
        checkpointMonth.setCheckpointcode(checkpointEntity.getDevicecode());
        checkpointMonth.setCheckpointname(checkpointEntity.getDevicename());
        checkpointMonth.setPassingcarnum(randomNum);
        checkpointMonth.setDistrictpassingcarnum((int) (0.6 * checkpointMonth.getPassingcarnum()));
        checkpointMonth.setDate(currentDay);
        checkpointMonth.setLocalcarnum((int) (0.9 * checkpointMonth.getDistrictpassingcarnum()));
        checkpointMonth.setNolocalcarnum(checkpointMonth.getDistrictpassingcarnum() - checkpointMonth.getLocalcarnum());
        checkpointMonth.setPetrolcarnum((int) (0.85 * checkpointMonth.getDistrictpassingcarnum()));
        checkpointMonth.setSmallcarnum((int) (0.65 * checkpointMonth.getPetrolcarnum()));
        checkpointMonth.setBigcarnum(checkpointMonth.getPassingcarnum() - checkpointMonth.getSmallcarnum());
        checkpointMonth.setElectriccarnum(checkpointMonth.getDistrictpassingcarnum() - checkpointMonth.getPetrolcarnum());
        checkpointMonth.setNormalcarnum((int) (0.95 * checkpointMonth.getDistrictpassingcarnum()));
        checkpointMonth.setSpecialcarnum(checkpointMonth.getDistrictpassingcarnum() - checkpointMonth.getNormalcarnum());
        //存入list中
        resultList.add(checkpointMonth);

    }
}
